package com.deng.performance.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.deng.common.SQLHelper;
import com.deng.common.vo.DispenseOrganizationVo;
import com.deng.common.vo.EmployeeVo;
import com.deng.performance.dao.IDispenseEmployee;
import com.deng.util.GlobalProperties;

public class DispenseEmployeeImpl extends SQLHelper implements
		IDispenseEmployee {

	public void updateByNewFunction(DispenseOrganizationVo vo) {
		try{
			System.out.println("更新个人奖金：   。。。。");
			Connection conn = getConnect();
			String sql = "select * from t_sys_stat_organization_vs_dispense_organization where id = "+vo.id;
			ResultSet rs = queryData(conn,sql);
			if(rs.next()){
				String formula = rs.getString("formula");
				if(formula == null){
					System.out.println("整组个人没有定义公式");
					return;//没定义公式
				}
				String cal_value = vo.cal_value;
				if(cal_value == null){
					cal_value = "0";
				}
				formula = formula.replaceAll(GlobalProperties.GET_DispenseBonusFormula, cal_value);
				sql = "select * from t_stat_employee_month " +
						"	where gongzuojixiaohesuandanyuan = " +vo.id+
						"	and stat_time ='"+vo.stat_time_str+"'";
//				PreparedStatement pst = 
//					conn.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
				ResultSet rs2 = queryData(conn, sql);
				EmployeeVo evo;
				while(rs2.next()){
					int employee_id = rs2.getInt("employee_id");
					evo = new EmployeeVo();
					evo.id = employee_id;
					evo.valid = rs2.getInt("valid");
					evo.name = rs2.getString("name");
					evo.work_id = rs2.getString("work_id");
					evo.cal_jingyingjixiao = rs2.getBigDecimal("cal_jingyingjixiao")!=null?rs2.getBigDecimal("cal_jingyingjixiao").toString():"0";
					evo.pay_jingyingjixiao = rs2.getBigDecimal("pay_jingyingjixiao")!=null?rs2.getBigDecimal("pay_jingyingjixiao").toString():"0";
					evo.pay_gongzuojixiao = rs2.getBigDecimal("pay_gongzuojixiao")!=null?rs2.getBigDecimal("pay_gongzuojixiao").toString():"0";
					String empFormula = formula.replaceAll("@employee_id", employee_id+"")
//								.replaceAll(GlobalProperties.GET_DispenseBonusFormula,"["+dispenseBonusFormula+"]")
								.replaceAll("@dispense_id", vo.id+"")
								.replaceAll("@stat_time", "'"+vo.stat_time_str+"'")
								.replaceAll("\\[", "\\(").replaceAll("\\]", "\\)")
								.replaceAll("\\＋", "\\+").replaceAll("\\－", "\\-")
								.replaceAll("\\×", "\\*").replaceAll("\\÷", "\\/")
								.replaceAll("\'\'","\'");
					ResultSet rs3 = queryData(conn, "select "+empFormula);
					if(rs3.next()){
						BigDecimal cal_employee = rs3.getBigDecimal(1);
						evo.cal_gongzuojixiao = cal_employee!=null?cal_employee.toString():"0";
					}else{
						evo.cal_gongzuojixiao = "0";
					}
					String updateSQL = "" ;
					if(evo.valid == 0){
						updateSQL = "update t_stat_employee_month " +
						"	set pay_gongzuojixiao  = "+evo.cal_gongzuojixiao+"," +
						"	cal_gongzuojixiao = "+evo.cal_gongzuojixiao+
						"	where gongzuojixiaohesuandanyuan = " +vo.id+
						"	and stat_time ='"+vo.stat_time_str+"'" +
						"	and employee_id = "+evo.id;
						
//						rs2.updateBigDecimal("pay_gongzuojixiao", new BigDecimal(evo.pay_gongzuojixiao));
//						rs2.updateBigDecimal("cal_gongzuojixiao", new BigDecimal(evo.cal_gongzuojixiao));						
					}else{
						updateSQL = "update t_stat_employee_month " +
						"	set cal_gongzuojixiao  = "+evo.cal_gongzuojixiao+
						"	where gongzuojixiaohesuandanyuan = " +vo.id+
						"	and stat_time ='"+vo.stat_time_str+"'" +
						"	and employee_id = "+evo.id;
//						rs2.updateBigDecimal("cal_gongzuojixiao", new BigDecimal(evo.cal_gongzuojixiao));
					}		
					updateData(conn, updateSQL);
//					rs2.updateRow(); 
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
	public void updateByOldFunction(DispenseOrganizationVo vo) {
		try{
//			Connection conn = getConnect();
//			String sql = " select t_sys_stat_organization_vs_dispense_organization.id, " +
//					"   t_sys_stat_organization_vs_dispense_organization.name," +
//					"	t_sys_stat_organization_vs_dispense_organization.formula as formula_new," +
//					"	t_stat_dispense_month.formula as formula_old," +
//					"	t_stat_dispense_month.cal_value,t_stat_dispense_month.pay_value," +
//					"	t_stat_dispense_month.put_value, t_stat_dispense_month.valid," +
//					"	 t_sys_stat_organization_vs_dispense_organization.related_organization_id as new_related_id," +
//					"	t_stat_dispense_month.related_organization_id as old_related_id," +
//					"	'"+statMonth+"' as stat_time" +
//					"	 from t_sys_stat_organization_vs_dispense_organization" +
//					"	left join t_stat_dispense_month" +
//					"	on t_sys_stat_organization_vs_dispense_organization.id = t_stat_dispense_month.dispense_id" +
//					"	and stat_time = '"+statMonth+"'" +
//					"	order by  t_sys_stat_organization_vs_dispense_organization.name";
//			ResultSet rs = queryData(conn,sql);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	

	public List<EmployeeVo> updateAndGetByNewFunction(DispenseOrganizationVo vo) {
		try{
			updateByNewFunction(vo);
			String sql = "select * from t_stat_employee_month " +
				"	where gongzuojixiaohesuandanyuan = " +vo.id+
				"	and stat_time ='"+vo.stat_time_str+"'";
			Connection conn = getConnect();
			ResultSet rs2 = queryData(conn, sql);
			EmployeeVo evo;
			List<EmployeeVo> l = new ArrayList<EmployeeVo>();
			while(rs2.next()){
				int employee_id = rs2.getInt("employee_id");
				evo = new EmployeeVo();
				evo.id = employee_id;
				evo.valid = rs2.getInt("valid");
				evo.name = rs2.getString("name");
				evo.work_id = rs2.getString("work_id");
				evo.stat_time = vo.stat_time_str;
				evo.pay_gongzuojixiao = rs2.getBigDecimal("pay_gongzuojixiao")!=null?rs2.getBigDecimal("pay_gongzuojixiao").toString():"0";
				evo.cal_gongzuojixiao = rs2.getBigDecimal("cal_gongzuojixiao")!=null?rs2.getBigDecimal("cal_gongzuojixiao").toString():"0";
				evo.pay_jingyingjixiao = rs2.getBigDecimal("pay_jingyingjixiao")!=null?rs2.getBigDecimal("pay_jingyingjixiao").toString():"0";
				evo.cal_jingyingjixiao = rs2.getBigDecimal("cal_jingyingjixiao")!=null?rs2.getBigDecimal("cal_jingyingjixiao").toString():"0";
				l.add(evo);
			}
			return l;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
